package cn.auto.controller;

import java.util.HashMap;
import java.util.List;

import com.jfinal.core.ActionKey;

import cn.auto.service.RoundService;
import cn.auto.service.RuninfoService;
import cn.auto.utility.HelperController;
import cn.auto.utility.Status;
import cn.auto.utility.Utilities;
import net.sf.json.JSONObject;

public class RoundController extends HelperController {
	
	RoundService round=new RoundService();
	RuninfoService run=new RuninfoService();
	String isRerun = null; 
	
	public void index() {
		String pid=getPara("pid");
		HashMap<Object, Object> result=round.getRounds(pid);
		String st = (String) result.get("status"); 
		HashMap<String, String> msg=new HashMap<>();
		msg.put(Status.SUCCESS, "获取成功!");
		msg.put(Status.NORECORD, "无记录!");
		msg.put(Status.FAILED, "获取失败!");
		responseResult(st, msg,(List<?>)result.get("body"));
	}
	
	
	/**
	 * 接收到前端传递的round名，启动该round的测试，发布测试消息到客户端，
	 * 并将round中的case名加入测试任务队列
	 */
	@ActionKey("/startRun")
	public void startRun() {
		String pid=getPara("pid");
		String rid=getPara("rid");
		String client=getPara("cli");
		isRerun=getPara("rr");
		
		if(client.equals("null")){
			client="all";
		}
		String[] msg = { "启动该轮测试成功!", "启动该轮测试失败!" };
		String st=run.addTaskQueue(pid, client,rid );

		responseResult(st, msg);
	}
	
	@ActionKey("/round/rerun")
	public void rerun() {
		String rid=getPara("rid");
		String client=getPara("cli");
		
		if(client.equals("null")){
			client="all";
		}
		String[] msg = { "启动运行失败脚本成功!", "启动运行失败脚本失败!" };
		String st=run.addFailedQueue(rid );
		responseResult(st, msg);
	}
	
	@ActionKey("/round/autoRerun")
	public void autoRerun() {
		String rid=getPara("rid");	

		String[] msg = { "启动运行失败脚本成功!", "启动运行失败脚本失败!" };
		String st=run.addFailedQueue(rid );
		responseResult(st, msg);
	}
	
	@ActionKey("/round/add")
	public void addRound() {
		JSONObject jo = readRequstJson();
		HashMap<String, String> addInfo = Utilities.jsonToHashMap(jo);
		String st = round.addRound(addInfo);
		String[] msg = { "添加测试轮次成功!", "添加测试轮次失败!" };
		responseResult(st, msg);
	}
	
	@ActionKey("/round/del")
	public void delRound() {
		String id=getPara("id");
		String st = round.delRound(id);
		String[] msg = { "删除测试轮次成功!", "删除测试轮次失败!" };
		responseResult(st, msg);
	}
}
